package com.frain.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/discovery")
public class DiscoveryClientController {

    @Autowired
    private DiscoveryClient client;

    @ResponseBody
    @RequestMapping("/github")
    public String getGitHub() {

        List<ServiceInstance> instances = client.getInstances("biz-provider-01");

        String host = instances.get(0).getHost();
        int port = instances.get(0).getPort();

        String responseValue = new RestTemplate().getForObject("http://" + host + ":" + port + "/demo/getFind",
                String.class);
        return responseValue;
    }

}
